package com.qiye.livevideo.common;


import io.swagger.annotations.ApiModelProperty;
/**
 * Created on 2019/4/21.
 *
 * @author meishijun
 */
public class ResultInfo<T> {

    @ApiModelProperty("返回信息")
    private String message;

    @ApiModelProperty("状态码 0成功 1失败 2未登录 3没有权限")
    private int retCode;

    @ApiModelProperty("返回数据")
    private T data;

    private ResultInfo(T data){
        this.retCode = 0;
        if(data instanceof String){
            this.message = String.valueOf(data);
        }else{
            this.message = "成功";
            this.data = data;
        }
    }

    private ResultInfo(T data, String message){
        this.retCode = 0;
        this.message = message;
        this.data = data;
    }

    private ResultInfo(CodeMsg cm) {
        if(cm == null){
            return;
        }
        this.retCode = cm.getRetCode();
        this.message = cm.getMessage();
    }
    /**
     * 成功时候的调用,传入data
     * @return
     */
    public static <T> ResultInfo<T> success(T data){
        return new ResultInfo<T>(data);
    }

    /**
     * 成功时候的调用,传入CodeMsg
     * @param cm
     * @param <T>
     * @return
     */
    public static <T> ResultInfo<T> success(CodeMsg cm){
        return new ResultInfo<T>(cm);
    }
    /**
     * 成功，不需要传入参数
     * @return
     */
    public static <T> ResultInfo<T> success(){
        return (ResultInfo<T>) success("");
    }
    /**
     * 成功时候的调用,自定义message
     * @return
     */
    public static <T> ResultInfo<T> success(T data,String message){
        return new ResultInfo<T>(data,message);
    }
    /**
     * 成功时候的调用,扩展消息参数
     * @param cm
     * @param msg
     * @return
     */
    public static <T> ResultInfo<T> success(CodeMsg cm,String msg){
        cm.setMessage(msg);
        return new ResultInfo<T>(cm);
    }
    /**
     * 失败时候的调用
     * @return
     */
    public static <T> ResultInfo<T> error(CodeMsg cm){
        return new ResultInfo<T>(cm);
    }
    /**
     * 失败时候的调用,扩展消息参数
     * @param cm
     * @param msg
     * @return
     */
    public static <T> ResultInfo<T> error(CodeMsg cm,String msg){
        cm.setMessage(msg);
        return new ResultInfo<T>(cm);
    }

    public T getData() {
        return data;
    }
    public String getMessage() {
        return message;
    }
    public int getRetCode() {
        return retCode;
    }

}
